package com.insist.service.impl;

import com.insist.dao.OrderDetailInfoDao;
import com.insist.dao.RewardInfoDao;
import com.insist.entity.OrderDetailInfo;
import com.insist.entity.ProductInfo;
import com.insist.entity.RewardInfo;
import com.insist.entity.UserInfo;
import com.insist.enums.CmmNoType;
import com.insist.model.RewardModel;
import com.insist.service.ProductService;
import com.insist.service.RewardService;
import com.insist.service.UserService;
import com.insist.util.BeanCopierUtils;
import com.insist.util.CmmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/7/5.
 */
@Service
public class RewardServiceImpl implements RewardService {

    @Autowired
    ProductService productService;
    @Autowired
    OrderDetailInfoDao orderDetailInfoDao;
    @Autowired
    RewardInfoDao rewardInfoDao;
    @Autowired
    UserService userService;

    @Override
    public void checkProducts(List<ProductInfo> productInfos) {
        for(ProductInfo productInfo:productInfos){
            if(productInfo.getBuyNum()>=productInfo.getTotalNum()){
                doReward(productInfo);
            }
        }
    }

    @Override
    public List<RewardModel> findRewardList() {
        List<RewardInfo> list = rewardInfoDao.findRewardListLimit(new PageRequest(0,5));
        List<RewardModel> rsList = new ArrayList<>();
        for(RewardInfo detailInfo:list){
            RewardModel detailInfoModel = new RewardModel();
            BeanCopierUtils.copyProperties(detailInfo, detailInfoModel);
            rsList.add(detailInfoModel);
            UserInfo userInfo = userService.getById(detailInfo.getUserId());
            detailInfoModel.setUserName(userInfo.getUsername());
            if(userInfo.getImage()!=null){
                detailInfoModel.setUserImg(userInfo.getImage());
            }
            ProductInfo productInfo = productService.getProductById(detailInfo.getProductId());
            detailInfoModel.setProductName(productInfo.getName());
            detailInfoModel.setProductImage(CmmUtils.replacePictureUrl(productInfo.getPicture()));
        }
        return rsList;
    }

    @Override
    public List<RewardModel> findProductReward(Long productId) {
        List<RewardInfo> list = rewardInfoDao.findByProductId(productId);
        List<RewardModel> rsList = new ArrayList<>();
        for(RewardInfo detailInfo:list){
            RewardModel detailInfoModel = new RewardModel();
            BeanCopierUtils.copyProperties(detailInfo, detailInfoModel);

            UserInfo userInfo = userService.getById(detailInfo.getUserId());
            detailInfoModel.setUserName(userInfo.getUsername());
            if(userInfo.getImage()!=null){
                detailInfoModel.setUserImg(userInfo.getImage());
            }

            ProductInfo productInfo = productService.getProductById(detailInfo.getProductId());
            detailInfoModel.setProductName(productInfo.getName());
            rsList.add(detailInfoModel);
        }
        return rsList;
    }

    @Override
    public List<RewardModel> findUserReward(Long loginUserId) {
        List<RewardInfo> list = rewardInfoDao.findByUserId(loginUserId);
        List<RewardModel> rsList = new ArrayList<>();
        for(RewardInfo detailInfo:list){
            RewardModel detailInfoModel = new RewardModel();
            BeanCopierUtils.copyProperties(detailInfo, detailInfoModel);

            UserInfo userInfo = userService.getById(detailInfo.getUserId());
            detailInfoModel.setUserName(userInfo.getUsername());
            if(userInfo.getImage()!=null){
                detailInfoModel.setUserImg(userInfo.getImage());
            }

            ProductInfo productInfo = productService.getProductById(detailInfo.getProductId());
            detailInfoModel.setProductName(productInfo.getName());
            detailInfoModel.setProductImage(CmmUtils.replacePictureUrl(productInfo.getPicture()));
            rsList.add(detailInfoModel);
        }
        return rsList;
    }

    /**
     * 分配中奖信息
     * @param productInfo
     */
    private void doReward(ProductInfo productInfo) {
        List<OrderDetailInfo> list = orderDetailInfoDao.findByProductIdAndProductNo(productInfo.getId(),productInfo.getProductNo());
        Integer total = productInfo.getTotalNum();
        Integer rewardNo = new Random().nextInt(total);
        OrderDetailInfo rewardDetailInfo = null;
        for(OrderDetailInfo info:list){
            rewardNo -= info.getNumber();
            if(rewardNo<0){
                rewardDetailInfo = info;
                break;
            }
        }
        RewardInfo rewardInfo = new RewardInfo();
        BeanCopierUtils.copyProperties(rewardDetailInfo, rewardInfo);
        rewardInfo.setCreateTime(new Date());
        rewardInfo.setId(null);
        rewardInfoDao.save(rewardInfo);

        //处理产品信息
        if(productInfo.getAutoReload()){
            productInfo.setBuyNum(0);
            productInfo.setProductNo(CmmUtils.createOrderNo(CmmNoType.PRODUCT));
            productService.save(productInfo);
        } else {
            productInfo.setIsFinish(true);
            productService.save(productInfo);
        }
    }
}
